package com.whfc.ms.api.md.service.impl;

import com.whfc.common.enums.AppWarnModuleType;
import com.whfc.common.result.PageData;
import com.whfc.emp.dto.AppWarnEmpRecordDTO;
import com.whfc.emp.enums.AppEmpWarnRuleType;
import com.whfc.emp.service.AppEmpWarnService;
import com.whfc.entity.dto.warn.AppWarnDTO;
import com.whfc.env.dto.EnvWarnRecordDTO;
import com.whfc.env.enums.EnvWarnRuleType;
import com.whfc.env.service.EnvWarnDataService;
import com.whfc.fim.dto.FimResultDTO;
import com.whfc.fim.enums.FimDeviceRuleType;
import com.whfc.fim.service.FimResultService;
import com.whfc.fse.dto.FseWarnRecordDTO;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.service.FseWarnService;
import com.whfc.mach.dto.warn.AppWarnMachRecordDTO;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.service.AppMachWarnService;
import com.whfc.ms.api.md.service.MdIndexBoardService;
import com.whfc.uni.dto.CarBoardAnalysisDTO;
import com.whfc.uni.service.CarInoutRecordService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: hw
 * @date: 2021-11-18 16:06
 * @description: 大屏首页接口
 */
@Service
public class MdIndexBoardServiceImpl implements MdIndexBoardService {

    @DubboReference(interfaceClass = CarInoutRecordService.class, version = "1.0.0")
    private CarInoutRecordService carInoutRecordService;

    @DubboReference(interfaceClass = AppMachWarnService.class, version = "1.0.0")
    private AppMachWarnService appMachWarnService;

    @DubboReference(interfaceClass = AppEmpWarnService.class, version = "1.0.0")
    private AppEmpWarnService appEmpWarnService;

    @DubboReference(interfaceClass = FseWarnService.class, version = "1.0.0")
    private FseWarnService fseWarnService;

    @DubboReference(interfaceClass = EnvWarnDataService.class, version = "1.0.0")
    private EnvWarnDataService envWarnDataService;

    @DubboReference(interfaceClass = FimResultService.class, version = "1.0.0")
    private FimResultService fimResultService;

    @Override
    public CarBoardAnalysisDTO carBoardAnalysis(Integer deptId, Date startDate, Date endDate) {
        return carInoutRecordService.boardAnalysis(deptId, startDate, endDate);
    }

    @Override
    public CarBoardAnalysisDTO carBoardAnalysis2(Integer deptId, Date startDate, Date endDate) {
        return carInoutRecordService.boardAnalysis2(deptId, startDate, endDate);
    }

    @Override
    public List<AppWarnDTO> getWarnList(Integer deptId) {
        List<AppWarnDTO> list = new ArrayList<>();
        // 获取设备报警记录
        PageData<AppWarnMachRecordDTO> machWarnPages = appMachWarnService
                .recordList(1, 10, deptId, null, null, null, null);
        for (AppWarnMachRecordDTO recordDTO : machWarnPages.getList()) {
            AppWarnDTO appWarnDTO = new AppWarnDTO();
            appWarnDTO.setModuleType(AppWarnModuleType.MACH.value());
            appWarnDTO.setModuleName("设备管理");
            appWarnDTO.setWarnId(recordDTO.getWarnId());
            appWarnDTO.setTriggerTime(recordDTO.getTriggerTime());
            AppMachWarnRuleType ruleType = AppMachWarnRuleType.parseValue(recordDTO.getRuleType());
            appWarnDTO.setRuleTypeName(ruleType.tittle());
            list.add(appWarnDTO);
        }

        //获取人员报警记录
        List<AppWarnEmpRecordDTO> empWarnPages = appEmpWarnService.getNewList(deptId);
        for (AppWarnEmpRecordDTO recordDTO : empWarnPages) {
            AppWarnDTO appWarnDTO = new AppWarnDTO();
            appWarnDTO.setModuleName("人员管理");
            appWarnDTO.setModuleType(AppWarnModuleType.EMP.value());
            appWarnDTO.setWarnId(recordDTO.getWarnId());
            appWarnDTO.setTriggerTime(recordDTO.getTriggerTime());
            AppEmpWarnRuleType ruleType = AppEmpWarnRuleType.parseByValue(recordDTO.getRuleType());
            appWarnDTO.setRuleTypeName(ruleType.tittle());
            list.add(appWarnDTO);
        }

        //获取特种设备报警记录
        PageData<FseWarnRecordDTO> fseWarnPages = fseWarnService
                .recordList(deptId, 1, 10, null, null, null, null, null);
        for (FseWarnRecordDTO recordDTO : fseWarnPages.getList()) {
            AppWarnDTO appWarnDTO = new AppWarnDTO();
            appWarnDTO.setModuleName("特种设备");
            appWarnDTO.setModuleType(AppWarnModuleType.FSE.value());
            appWarnDTO.setWarnId(recordDTO.getId());
            appWarnDTO.setTriggerTime(recordDTO.getTriggerTime());
            FseWarnRuleType ruleType = FseWarnRuleType.parseByValue(recordDTO.getRuleType());
            appWarnDTO.setRuleTypeName(ruleType.tittle());
            list.add(appWarnDTO);
        }

        //获取环境报警记录
        PageData<EnvWarnRecordDTO> envWarnPages = envWarnDataService.getEnvWarnRecordList(deptId, null, null, 1, 10);
        for (EnvWarnRecordDTO recordDTO : envWarnPages.getList()) {
            AppWarnDTO appWarnDTO = new AppWarnDTO();
            appWarnDTO.setModuleName("环境监测");
            appWarnDTO.setModuleType(AppWarnModuleType.ENV.value());
            appWarnDTO.setWarnId(recordDTO.getId());
            appWarnDTO.setTriggerTime(recordDTO.getTriggerTime());
            EnvWarnRuleType ruleType = EnvWarnRuleType.parseValue(recordDTO.getRuleType());
            appWarnDTO.setRuleTypeName(ruleType.tittle());
            list.add(appWarnDTO);
        }

        //获取智能监控报警
        PageData<FimResultDTO> fimWarnPages = fimResultService.list(1, 10, deptId, null, null);
        for (FimResultDTO recordDTO : fimWarnPages.getList()) {
            AppWarnDTO appWarnDTO = new AppWarnDTO();
            appWarnDTO.setModuleName("智能监控");
            appWarnDTO.setModuleType(AppWarnModuleType.FIM.value());
            appWarnDTO.setWarnId(recordDTO.getResultId());
            appWarnDTO.setTriggerTime(recordDTO.getTime());
            FimDeviceRuleType ruleType = FimDeviceRuleType.parseByValue(recordDTO.getAlgCheckType());
            appWarnDTO.setRuleTypeName(ruleType.tittle());
            list.add(appWarnDTO);
        }
        return list;
    }
}
